package com.zxiw.zxedu.recyclerview.adapter

import android.R
import android.util.SparseArray
import android.view.View
import androidx.annotation.IdRes


/**
 * @author zhangxiaoming
 * @date 2023/8/18
 * @description
 */
open class BaseRecyclerViewHolder(itemView: View, val layoutId: Int) : BH(itemView) {
    private val mViews = SparseArray<View>()

    init {
        //因为Sticky也要用到tag,所有采用多tag的方式处理，产生一个唯一的key值
        itemView.setTag("holder".hashCode(), this)
    }

    @Suppress("UNCHECKED_CAST")
    fun <R : View> getView(@IdRes viewId: Int): R? {
        var view: View? = mViews.get(viewId)
        if (view == null) {
            view = itemView.findViewById(viewId)
            view?.let {
                mViews.put(viewId, it)
            }
        }
        return view as R
    }
}